/**
 * Copyright (C) 2015-2016 The YunOS Project. All rights reserved.
 */

#include "tee_trm.h"
#include "tee_trm_priv.h"

#if CONFIG_ID2_RSA_KEY
extern void _convert_buf_to_bndata(const uint8_t *src, int32_t src_bytes,
                                   uint32_t *dst, int32_t dst_words);

extern uint32_t _g_id2_rsa_c_acc[ID2_KEY_BITS >> 5];
extern uint32_t _g_id2_rsa_n[ID2_KEY_BITS >> 5];
extern uint32_t _g_id2_rsa_e[ID2_KEY_BITS >> 5];
extern uint32_t _g_id2_rsa_d[ID2_KEY_BITS >> 5];
uint8_t _g_id2_rsa_fixed_raw_e[3] = {0x01, 0x00, 0x01};
#elif CONFIG_ID2_AES_KEY
extern uint8_t _g_id2_aes_key[ID2_AES_KEY_MAX_BYTES];
extern uint32_t _g_id2_aes_key_bits;
#endif /* CONFIG_ID2_AES_KEY */


/************************************************/
/*         FAKE ID2 KEY and ID                  */
/************************************************/
#if CONFIG_ID2_RSA_KEY
#if TRM_FAKE_KEY_SEL0
char ID[18] = "Y0482208A0D6E011A";

uint8_t RSA_N[] = {
    0xB9, 0xC2, 0x6A, 0x88, 0x90, 0xB0, 0xE1, 0x1B,
    0xD3, 0x3F, 0x93, 0x99, 0xF8, 0xCB, 0xC4, 0xF2,
    0x88, 0xB8, 0x56, 0x9A, 0x93, 0x24, 0x0B, 0x4A,
    0x75, 0xB4, 0x8D, 0x6C, 0x32, 0x40, 0x52, 0xA7,
    0x3D, 0x2B, 0x37, 0xCE, 0x57, 0x80, 0xFD, 0xA5,
    0x08, 0xC7, 0x4A, 0x77, 0x94, 0x34, 0xBA, 0x56,
    0x9F, 0xBA, 0x2E, 0xBA, 0x20, 0x1E, 0x80, 0x4A,
    0x09, 0x40, 0x07, 0xF5, 0x1D, 0xB6, 0xFC, 0x8E,
    0x5C, 0x4E, 0x02, 0x01, 0xD5, 0x97, 0x14, 0x83,
    0x2A, 0x32, 0x90, 0x4E, 0xED, 0xDC, 0x6F, 0xD6,
    0x0A, 0x1F, 0x44, 0x6A, 0xB1, 0xFE, 0x42, 0xBD,
    0xBF, 0xD7, 0x6F, 0xB5, 0x9B, 0x78, 0x5F, 0x19,
    0x07, 0x15, 0x4E, 0x65, 0xD2, 0x72, 0x09, 0x0C,
    0x14, 0xAD, 0xE8, 0xC7, 0x76, 0x25, 0xE8, 0x94,
    0xF8, 0xEE, 0x19, 0x93, 0xDF, 0xC9, 0x34, 0xEE,
    0x42, 0x1A, 0xA6, 0xA8, 0x70, 0xE2, 0xC0, 0x99
};
uint8_t RSA_D[] = {
    0x0D, 0x59, 0xA1, 0x9A, 0x6A, 0xFB, 0xAB, 0x51,
    0xFA, 0x9E, 0xA9, 0x16, 0xF9, 0xC0, 0x7B, 0x49,
    0x0A, 0xF6, 0x64, 0x92, 0xFB, 0x5C, 0x2D, 0xCB,
    0x8E, 0x26, 0x09, 0xBB, 0xEA, 0xE8, 0xA0, 0x88,
    0x10, 0x67, 0x58, 0xBD, 0x65, 0xA0, 0x89, 0x9A,
    0x20, 0xC5, 0x61, 0xDE, 0xB2, 0x32, 0x0F, 0xEA,
    0x45, 0xD4, 0x75, 0xA3, 0x3E, 0xC7, 0xB2, 0x64,
    0x9F, 0xC1, 0x90, 0x50, 0xD7, 0x6F, 0xDD, 0x3A,
    0xA6, 0xA3, 0x52, 0xED, 0xB6, 0xFC, 0x2B, 0x61,
    0xA5, 0xDB, 0xE4, 0xC7, 0xF8, 0x21, 0x30, 0xEF,
    0x7F, 0x21, 0x1E, 0x5B, 0x96, 0x45, 0x43, 0x4B,
    0x74, 0x98, 0xC6, 0x1B, 0xD9, 0x66, 0x0A, 0x64,
    0x41, 0x5F, 0xF6, 0xE0, 0x87, 0xD6, 0xFA, 0x00,
    0x4D, 0x76, 0x9B, 0x4E, 0x36, 0xC5, 0x8A, 0x10,
    0x8C, 0x61, 0xA1, 0x41, 0xB0, 0x8B, 0x25, 0xEC,
    0x19, 0x25, 0x89, 0x45, 0x8E, 0xFB, 0xCF, 0xB9
};
#else /* !TRM_FAKE_KEY_SEL0 */
char ID[18] = "Y00F300016247B17C";

uint8_t RSA_N[] = {
    0xC6, 0x99, 0x83, 0xD1, 0xC4, 0xAE, 0x92, 0xA4,
    0xBF, 0x2E, 0x36, 0x31, 0xFA, 0x5D, 0x03, 0xCD,
    0x1B, 0x3F, 0x52, 0x01, 0x06, 0xD2, 0x3E, 0xC4,
    0x3A, 0x32, 0xC9, 0x35, 0x17, 0x6D, 0x9F, 0x9E,
    0x71, 0x2F, 0x6F, 0x3B, 0xD0, 0xE8, 0xA6, 0x80,
    0x83, 0x60, 0xD5, 0x8B, 0x3D, 0xAD, 0xFF, 0xFF,
    0x62, 0x1A, 0x4C, 0xD5, 0x04, 0x43, 0x08, 0x6B,
    0x57, 0x3B, 0x63, 0xF9, 0x48, 0x5A, 0x86, 0x4B,
    0xAE, 0x4E, 0xC9, 0x6D, 0xF3, 0xAA, 0x28, 0x41,
    0xB9, 0xA6, 0x2F, 0x0E, 0xD6, 0x18, 0x59, 0x76,
    0x00, 0xBE, 0x61, 0xAE, 0x8A, 0x1A, 0xB9, 0x23,
    0x29, 0xE6, 0xBD, 0xA0, 0xBA, 0xF6, 0x0D, 0x2B,
    0xA6, 0xDE, 0x16, 0xC8, 0xE9, 0x82, 0x34, 0x71,
    0x72, 0x13, 0x63, 0x64, 0x74, 0x85, 0x70, 0xC7,
    0xE8, 0x8A, 0xDA, 0x7F, 0x42, 0x33, 0x7A, 0x98,
    0x8C, 0x72, 0x66, 0x9D, 0x85, 0x5E, 0xF3, 0x3D
};

uint8_t RSA_D[] = {
    0x2C, 0x70, 0xA3, 0xAF, 0xA7, 0xEC, 0x4C, 0x82,
    0x33, 0x09, 0x9B, 0x34, 0xF9, 0xCA, 0x20, 0x94,
    0x9F, 0xF9, 0x3E, 0x57, 0x5E, 0x78, 0x63, 0x75,
    0x33, 0x74, 0xB3, 0xD3, 0x7E, 0x87, 0x98, 0x45,
    0x33, 0x1C, 0xA3, 0x2E, 0x66, 0x63, 0xF9, 0x06,
    0x1E, 0xDF, 0xE7, 0x48, 0xFE, 0x27, 0x56, 0x76,
    0x86, 0x34, 0x4E, 0x67, 0x08, 0x4A, 0x1B, 0xB3,
    0x31, 0x54, 0xE7, 0xDD, 0x1B, 0x54, 0xAE, 0x62,
    0x12, 0x18, 0xCA, 0x43, 0x28, 0xA1, 0x7A, 0x3F,
    0xB6, 0x2C, 0xA2, 0x3D, 0xED, 0x2D, 0x7E, 0x96,
    0xE0, 0x12, 0x6F, 0xC6, 0x86, 0x20, 0x6B, 0x5A,
    0x5E, 0x76, 0x79, 0x0C, 0x38, 0x80, 0x18, 0x50,
    0xA6, 0x42, 0x8C, 0xB7, 0xC3, 0x17, 0xEF, 0xB4,
    0xB8, 0xAE, 0x74, 0xDF, 0x0F, 0x69, 0xCD, 0xB5,
    0x73, 0x3E, 0xD4, 0x75, 0x27, 0x1D, 0x94, 0x91,
    0x8A, 0x1A, 0xA1, 0x8C, 0x08, 0x4A, 0x15, 0xE1
};
#endif /* TRM_FAKE_KEY_SEL0 */
#endif /* CONFIG_ID2_RSA_KEY */

#if CONFIG_ID2_AES_KEY
char ID[24] = "00FFFFFFFFFFFFA6F67AB700";
#if CONFIG_AES_256_BIT
#define FAKE_AES_BITS 256
uint8_t AES_KEY[] = {
    0xa5, 0xd5, 0xae, 0x48, 0xc3, 0xbc, 0x31, 0xdd,
    0x2b, 0xac, 0xd2, 0xff, 0xa3, 0x9d, 0x34, 0xf5,
    0x0f, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
    0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
};
#elif CONFIG_AES_192_BIT
#define FAKE_AES_BITS 192
uint8_t AES_KEY[] = {
    0xa5, 0xd5, 0xae, 0x48, 0xc3, 0xbc, 0x31, 0xdd,
    0x2b, 0xac, 0xd2, 0xff, 0xa3, 0x9d, 0x34, 0xf5,
    0x0f, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
};
#elif CONFIG_AES_128_BIT
#define FAKE_AES_BITS 128
uint8_t AES_KEY[] = {
0xa5, 0xd5, 0xae, 0x48, 0xc3, 0xbc, 0x31, 0xdd,
0x2b, 0xac, 0xd2, 0xff, 0xa3, 0x9d, 0x34, 0xf5,
};
#endif /* CONFIG_AES_256_BIT */
#endif /* CONFIG_ID2_AES_KEY */

int32_t trm_get_key_data(uint32_t key_type, void *data, uint32_t *size)
{
    if (data == 0) {
        TRM_DRV_DBG_E("trm_get_key_data: bad arg\n");
        return -1;
    }

    if (key_type == KEY_TYPE_ID2_ID) {
        if (*size < sizeof(ID) - 1) {
            TRM_DRV_DBG_E("trm_get_key_data: ID2_ID size too short(0x%08x)\n", *size);
            return -1;
        }
        memcpy(data, ID, sizeof(ID) - 1);
    } else {
        TRM_DRV_DBG_E("trm_get_key_data: bad arg\n");
        return -1;
    }
    return 0;
}

#if CONFIG_ID2_RSA_KEY
int32_t tee_trm_read_rsa_key(void)
{
    /* convert _g_id2_rsa_n and _g_id2_rsa_d and _g_id2_rsa_e */
    _convert_buf_to_bndata((const uint8_t *)RSA_N, sizeof(RSA_N),
                           (uint32_t *)_g_id2_rsa_n, (ID2_KEY_BITS >> 5));
    _convert_buf_to_bndata((const uint8_t *)RSA_D, sizeof(RSA_D),
                           (uint32_t *)_g_id2_rsa_d, (ID2_KEY_BITS >> 5));
    _convert_buf_to_bndata((const uint8_t *)_g_id2_rsa_fixed_raw_e, sizeof(_g_id2_rsa_fixed_raw_e),
                           (uint32_t *)_g_id2_rsa_e, (ID2_KEY_BITS >> 5));

    return 0;
}

#elif CONFIG_ID2_AES_KEY
int32_t tee_trm_read_sym_key(uint32_t type)
{
    if (type == TEE_TRM_AES) {
        memcpy(_g_id2_aes_key, AES_KEY, FAKE_AES_BITS >> 3);
        _g_id2_aes_key_bits = FAKE_AES_BITS;
    } else {
        TRM_DRV_DBG_E("not support yet\n");
        return -1;
    }

    return 0;
}
#endif /* CONFIG_ID2_AES_KEY */

